<!DOCTYPE html>
<html lang="zh-CN"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>添加商品页</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" th:href="@{layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{style/admin.css}" media="all">
    <!--<link rel="stylesheet" th:href="@{/style/admin.css}" media="all">-->
    <script type="text/javascript" charset="utf-8" th:src="@{/ueditor/ueditor.config.js}"></script>
    <script type="text/javascript" charset="utf-8" th:src="@{/ueditor/ueditor.all.min.js}"></script>
    <script type="text/javascript" charset="utf-8" th:src="@{/ueditor/lang/zh-cn/zh-cn.js}"></script>
    <script type="text/javascript" th:src="@{/laydate/laydate.js}"></script>
    <style type="text/css">
        .product-price {
            width: 50px;
            border: none;
        }

        #editor {
            margin-left: 120px;
        }

        .one-img {
            /*border: 1px solid red;*/
            height: 100px;
            width: 100px;
            margin-left: 110px;
        }

        .Fengetext {
            color: red;
        }

        .layui-card-header .layui-icon {
            line-height: initial;
            position: absolute;
            right: 0px;
            top: 50%;
            margin-top: -15px;
        }

        .upaction {
            background: red;
        }

        blockquote, body, button, dd, div, dl, dt, form, h1, h2, h3, h4, h5, h6, input, li, ol, p, pre, td, textarea, th, ul {
            margin: 0;
            padding: 1px;
            -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
        }
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-container"></div>
            <div class="layui-form-item">
                <div class="layui-form-item">
                    <form name="form" class="form">
                        <label class="layui-form-label">商品类别</label>
                        <div class="layui-input-block">
                            <div class="layui-input-inline">
                                <select name="quiz1" id="yj" lay-verify="required" lay-filter="selectyj">
                                    <option value="0">请选择分类</option>
                                </select>
                            </div>
                            <div class="layui-input-inline">
                                <select name="quiz2" id="rj" lay-filter="selectrj">
                                </select>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">分类图</label>
                            <div class="layui-upload">
                                <button type="button" class="layui-btn" id="classImg">上传分类图</button>
                                <div class="layui-upload-list  one-img">
                                    <img class="layui-upload-img" id="classImg_show" width="100px" height="100px">
                                    <p id="demo1Text"></p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>
<div class="layui-form-item">
    <div class="layui-input-block">
        <button class="layui-btn addproduct" lay-submit="" lay-filter="demo1" onclick="saveClassfyImg()">保存分类图</button>
    </div>
</div>
</div>
</div>
</body>
<script th:src="@{layui/jquery.min.js}"></script>
<script th:src="@{layui/layui.js}"></script>
<script>

    var rjId;
    loadyj();//查询一级分类
    $(document).ready(function () {
        layui.use('form', function () {
            var form = layui.form;
            form.on('select(selectyj)', function (data) {
                var yjId = data.value;
                var yjIds = parseInt(yjId);
                if (yjIds != 0) {
                    loadrj(yjId);
                }
                ////调用二级
            });
            // form.render();
            //
            form.on('select(selectrj)', function(data){
                rjId=data.value;
                var yjId=data.value;
            });

        });

    });

    /*商品分类查询*/
    function loadyj() {
        var str = "";
        $.ajax({
            async: false,
            url: "/selectYJClassify",
            success: function (result) {
                var data = result;
                for (var index in data) {
                    str = str + "<option value='" + data[index].classId + "'>" + data[index].classifyName + "</option>";
                }
                $("#yj").append(str);
            }
        });
    }

    function loadrj(id) {
        var str = "";
        $.ajax({
            url: "/selectRJClassify?classId=" + id,
            success: function (result) {
                var data = result;
                $('#rj').empty();
                for (var index in data) {
                    str = str + "<option value='" + data[index].classId + "'>" + data[index].classifyName + "</option>";
                }
                $("#rj").append(str);
                layui.use('form', function () {
                    var form = layui.form;
                    form.render();
                })
            }
        })
    }


    function saveClassfyImg() {

        if(rjId==null||rjId==""){
            layer.alert("请选择商品分类");
            return;}
        productNo = $('#productNo').val();
        var classifyId=rjId;

        $.ajax({
            type:'post',
            url: "/updateClassfyImgs",
            data: {
                classId: classifyId
            },
            success: function (result) {

                if (result.msg=='1') {

                    layer.alert("保存成功！");
                } else {
                    layer.alert("保存失败！");
                }
            }
        })
    }

    //上传规格图片
    layui.use('upload', function () {
        var $ = layui.jquery
            , upload = layui.upload;
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#classImg'
            , url: '/addClassfyImgs'
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#classImg_show').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.msg != '1') {
                    return layer.msg('上传失败!');
                }
                //上传成功
                layer.msg('上传成功！');
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
            ,size:1024
        });



    });


</script>
</html>

